//
//  ShowMessageViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/21.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ShowMessageViewController.h"
#import "ZWYNetTool.h"
#import "ShowMessageModel.h"
#import "ShowMessageRes.h"
#import "ShowMessageTableViewCell.h"
#import "ShowDetailViewController.h"

#define kShowMessageTableViewCellName @"ShowMessageTableViewCellNameReuse"
@interface ShowMessageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) ShowMessageModel *model;
@end

@implementation ShowMessageViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"查看全部消息";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = back;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _pageNum = 1;
    NSString *bodyString = [NSString stringWithFormat:@"addtime=0&page=%ld", _pageNum];
    NSString *bodyLength = [NSString stringWithFormat:@"%@", bodyString];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *uuid = [user objectForKey:@"uuid"];
    NSString *uid = [user objectForKey:@"uid"];
    NSDictionary *dic = @{@"uuid" : uuid,
                          @"version" : @"2.50",
                          @"uid" : uid,
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : bodyLength};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=user&a=getshownewsAll" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [ShowMessageModel modelWithDic:result];
        NSLog(@"%@", result);
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    [_tableView registerNib:[UINib nibWithNibName:@"ShowMessageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kShowMessageTableViewCellName];
}
- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kShowMessageTableViewCellName];
    cell.res = _model.res[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowDetailViewController *vc = [[ShowDetailViewController alloc] init];
    vc.idString = _model.res[indexPath.row].sid;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
